<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />

			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索快递单号、快递公司、订单编号" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="expressList-list" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, reactive, ref } from "vue";

const { service } = useCool();
let expressOptions = ref<any>([]);

// 选项
const options = reactive({
	labelsList: [
		{ label: "已发货", value: 1, type: "danger" },
		{ label: "已签收", value: 2, type: "info" },
		{ label: "已完成", value: 3, type: "success" }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "快递单号",
			prop: "trackingNumber",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "快递公司",
			prop: "courierCompany",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "订单图片",
			prop: "orderImage",
			component: { name: "cl-upload", props: { multiple: true } }
		},
		{
			label: "订单编号",
			prop: "orderNumber",
			component: { name: "el-select", options: expressOptions, props: { clearable: true } },
			required: true
		},
		{
			label: "订单状态",
			prop: "status",
			component: {
				name: "el-radio-group",
				options: options.labelsList
			},
			col: {
				span: 12
			},
			value: 1
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "快递单号", prop: "trackingNumber", minWidth: 140 },
		{ label: "快递公司", prop: "courierCompany", minWidth: 140 },
		{
			label: "订单图片",
			prop: "orderImage",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{ label: "订单编号", prop: "orderNumber", minWidth: 140 },
		{ label: "订单状态", prop: "status", minWidth: 140 },
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.expressList.list
	},
	(app) => {
		app.refresh();
	}
);

const GetExpressOptions = async () => {
	let data = await service.order.info.getOrderNumList();
	expressOptions.value = data.map((o: any) => {
		return {
			value: o.orderNumber,
			label: o.orderNumber + `(${o.product})`
		};
	});
};

onMounted(() => {
	GetExpressOptions();
});

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
